package com.vanrui.pcp.autocode;

import java.util.List;

import org.apache.log4j.Logger;

import com.vanrui.pcp.autocode.model.entry.ProjectClass;
import com.vanrui.pcp.autocode.template.DataFactory;
import com.vanrui.pcp.autocode.template.DataUtil;
import com.vanrui.pcp.autocode.template.Environment;
import com.vanrui.pcp.autocode.template.Process;

public class AutoCode {
	
	private final static Logger logger = Logger.getLogger(AutoCode.class);
	
	public AutoCode(){
		try {
			 logger.info("*****START****************************************************");
			 DataUtil  dataUtil = new DataUtil();
			 List<DataFactory> factorys = dataUtil.getFactorys();
			 for (DataFactory factory : factorys) {
				 logger.info("----------------- Table："+factory.getTable().getTableName()+" 开始。。。-------------------");
				 create(factory);
				 logger.info("----------------- Table："+factory.getTable().getTableName()+" 结束！！！-------------------");
			 }
			 logger.info("喵(>^ω^<)喵  OK!!!运行结束!");
		} catch (Exception e) {
			logger.error("异常：", e);
			logger.error("凸(艹皿艹 )!! 工具产生异常，已终止运行！");
		}
		logger.info("*****END****************************************************");
	}
	
	private void create(DataFactory factory) throws Exception{
		logger.info("初始化环境  ...");
		 Environment evnm = new Environment(factory.getVersion());
		 evnm.setTemplateDirectory(factory.getTemplateDirectory());
		 evnm.setVariableMap(factory.getVariableMap());
		 Process process = new Process(evnm);
		 logger.info("环境初始化已OK!");
		 
		 logger.info("准备数据  ...");
		 List<ProjectClass> projectClazzs = factory.getProjectClazzs();
		 process.setProjectClazzs(projectClazzs);
		 logger.info("数据准备已OK!");
		 
		 logger.info("执行命令  ...");
		 process.start();//开始生成类文件
	}

}
